package rainy.file.synchronization.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

/**
 * Created by MANGOCOOL on 2015/10/30.
 */
public class SimpleChatServerHandler extends SimpleChannelInboundHandler<String> {

	public static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
		Channel incoming = ctx.channel();
		for (Channel channel : channels) {
			channel.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " 加入\n");
		}
		channels.add(ctx.channel());
	}

	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
		Channel incoming = ctx.channel();
		for (Channel channel : channels) {
			channel.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " 离开\n");
		}
		channels.remove(ctx.channel());
	}

	// 优先级高于messageReceived方法，有了这个方法就会屏蔽messageReceived方法
	// @Override
	// public void channelRead(ChannelHandlerContext ctx, Object msg) throws
	// Exception {
	// System.out.println("channelRead");
	// Channel incoming = ctx.channel();
	// for (Channel channel : channels) {
	// if (channel != incoming){
	// channel.writeAndFlush("[" + incoming.remoteAddress() + "]" + msg + "\n");
	// } else {
	// channel.writeAndFlush("server: " + msg + "\n");
	// }
	// }
	// }

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		Channel incoming = ctx.channel();
		System.out.println("client " + incoming.remoteAddress() + " 在线");
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		Channel incoming = ctx.channel();
		System.out.println("client " + incoming.remoteAddress() + " 掉线");
	}

	@Override
	protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
		Channel incoming = ctx.channel();
		for (Channel channel : channels) {
			if (channel != incoming) {
				// System.out.println("[" + incoming.remoteAddress() + "] " +
				// msg);
				channel.writeAndFlush("[" + incoming.remoteAddress() + "] " + msg + "\n");
			} else {
				// System.out.println("server: " + msg);
				channel.writeAndFlush("server: " + msg + "\n");
			}
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		Channel incoming = ctx.channel();
		System.out.println("client " + incoming.remoteAddress() + " 异常");
		// 当出现异常就关闭连接
		cause.printStackTrace();
		ctx.close();
	}

}